package com.dinree.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;

import com.base.library.BaseFragment;
import com.base.library.widget.CompetitionWebChromeClient;
import com.dinree.R;
import com.dinree.bean.UserBean;
import com.dinree.databinding.FragmentShopcarBinding;
import com.dinree.utils.Constant;
import com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.sdk.WebView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 谢铖志 on 2017/12/8 16:07
 * @version V1.0
 * @modificationHistory
 * @modify by user: 谢铖志 on 2017/12/8 16:07
 */

public class ShoppingCartFragment extends BaseFragment {
    private FragmentShopcarBinding fragmentShopcarBinding;
    private UserBean userBean;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_shopcar;
    }

    @Override
    public void initData(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
        fragmentShopcarBinding = getViewDataBinding();
        userBean = realm.findFirst(UserBean.class);
        if (userBean != null) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("user_id", userBean.user_id);
            map.put("user_token", userBean.user_token);
            fragmentShopcarBinding.webView.loadUrl(Constant.WEB_HOST + "front/#/cart", map);
        } else {
            fragmentShopcarBinding.webView.loadUrl(Constant.WEB_HOST + "front/#/cart");
        }
        fragmentShopcarBinding.webView.addJavascriptInterface(new JSHook(), "dinree");
        fragmentShopcarBinding.webView.setWebChromeClient(new CompetitionWebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                super.onProgressChanged(view, newProgress);

            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                fragmentShopcarBinding.tvTitle.setText(title);
            }

            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissionsCallback callback) {
                callback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, callback);
            }

            @Override
            public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
                super.onShowCustomView(view, customViewCallback);
            }
        });
        fragmentShopcarBinding.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragmentShopcarBinding.webView.loadUrl("javascript:toggleEditor(true)");
            }
        });
        updatePage();
    }

    public void updatePage() {
        if (fragmentShopcarBinding != null) {
            fragmentShopcarBinding.webView.loadUrl("javascript:updatePage()");
        }
    }

    public class JSHook {
        @JavascriptInterface
        public void setEditor(boolean b) {
            mContext.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (!b) {
                        fragmentShopcarBinding.edit.setText("编辑");
                        fragmentShopcarBinding.edit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                fragmentShopcarBinding.webView.loadUrl("javascript:toggleEditor(true)");
                            }
                        });
                    } else {
                        fragmentShopcarBinding.edit.setText("完成");
                        fragmentShopcarBinding.edit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                fragmentShopcarBinding.webView.loadUrl("javascript:toggleEditor(false)");
                            }
                        });
                    }
                }
            });
        }

        @JavascriptInterface
        public void setTitle(String title) {
            if (fragmentShopcarBinding != null) {
                fragmentShopcarBinding.tvTitle.setText(title);
            }
        }

    }
}
